@echo off
:: AISH 跨平台编译脚本 (Windows版本)
:: 支持 Windows, Linux, macOS 多架构编译

setlocal enabledelayedexpansion

:: 项目信息
set "APP_NAME=aish"
set "OUTPUT_DIR=dist"
set "SOURCE_FILE=cmd\main.go"
set "DEV_MODE=false"

:: 获取版本信息
for /f "delims=" %%i in ('git describe --tags --always --dirty 2^>nul') do set "VERSION=%%i"
if "!VERSION!"=="" set "VERSION=dev"

:: 获取构建时间
for /f "delims=" %%i in ('powershell -Command "Get-Date -UFormat '%%Y-%%m-%%dT%%H:%%M:%%SZ'"') do set "BUILD_TIME=%%i"

:: 获取Go版本
for /f "tokens=3" %%i in ('go version 2^>nul') do set "GO_VERSION=%%i"

:: 显示帮助信息
:show_help
echo AISH 跨平台编译脚本 (Windows版本)
echo.
echo 用法: %~nx0 [选项] [平台]
echo.
echo 选项:
echo   -h, --help     显示帮助信息
echo   -v, --version  显示版本信息
echo   -c, --clean    清理构建目录
echo   --dev          开发模式编译（不优化）
echo.
echo 平台:
echo   all            编译所有平台 (默认)
echo   windows        编译 Windows (amd64, 386)
echo   linux          编译 Linux (amd64, arm64, 386)
echo   macos          编译 macOS (amd64, arm64)
echo   current        仅编译当前平台
echo.
echo 示例:
echo   %~nx0                    # 编译所有平台
echo   %~nx0 windows            # 仅编译 Windows
echo   %~nx0 --clean            # 清理构建目录
echo   %~nx0 --dev linux        # 开发模式编译 Linux
goto :eof

:: 日志函数
:log_info
echo [INFO] %~1
goto :eof

:log_success
echo [SUCCESS] %~1
goto :eof

:log_warning
echo [WARNING] %~1
goto :eof

:log_error
echo [ERROR] %~1
goto :eof

:: 清理构建目录
:clean_build
call :log_info "清理构建目录: %OUTPUT_DIR%"
if exist "%OUTPUT_DIR%" (
    rmdir /s /q "%OUTPUT_DIR%"
    call :log_success "构建目录已清理"
) else (
    call :log_info "构建目录不存在"
)
goto :eof

:: 检查依赖
:check_dependencies
go version >nul 2>&1
if errorlevel 1 (
    call :log_error "Go 编译器未安装或不在 PATH 中"
    exit /b 1
)

call :log_info "Go 版本: %GO_VERSION%"
call :log_info "项目版本: %VERSION%"
call :log_info "构建时间: %BUILD_TIME%"
goto :eof

:: 编译单个目标
:build_target
set "target_os=%~1"
set "target_arch=%~2"
set "output_name=%~3"

:: Windows 需要 .exe 扩展名
if "%target_os%"=="windows" (
    set "output_name=!output_name!.exe"
)

set "output_path=%OUTPUT_DIR%\%target_os%-%target_arch%\!output_name!"

call :log_info "编译 %target_os%/%target_arch% -> !output_path!"

:: 创建输出目录
if not exist "%OUTPUT_DIR%\%target_os%-%target_arch%" (
    mkdir "%OUTPUT_DIR%\%target_os%-%target_arch%"
)

:: 编译标志
set "ldflags="
if "%DEV_MODE%"=="false" (
    set "ldflags=-s -w"
)

:: 设置环境变量并编译
set "GOOS=%target_os%"
set "GOARCH=%target_arch%"
go build -trimpath -ldflags="!ldflags!" -o "!output_path!" "%SOURCE_FILE%"

if errorlevel 1 (
    call :log_error "编译失败: %target_os%/%target_arch%"
    exit /b 1
) else (
    for %%F in ("!output_path!") do set "file_size=%%~zF"
    call :log_success "编译成功: %target_os%/%target_arch% (!file_size! bytes)"
)
goto :eof

:: 编译 Windows
:build_windows
call :log_info "开始编译 Windows 平台"
call :build_target "windows" "amd64" "%APP_NAME%"
if errorlevel 1 exit /b 1
call :build_target "windows" "386" "%APP_NAME%"
if errorlevel 1 exit /b 1
goto :eof

:: 编译 Linux
:build_linux
call :log_info "开始编译 Linux 平台"
call :build_target "linux" "amd64" "%APP_NAME%"
if errorlevel 1 exit /b 1
call :build_target "linux" "arm64" "%APP_NAME%"
if errorlevel 1 exit /b 1
call :build_target "linux" "386" "%APP_NAME%"
if errorlevel 1 exit /b 1
goto :eof

:: 编译 macOS
:build_macos
call :log_info "开始编译 macOS 平台"
call :build_target "darwin" "amd64" "%APP_NAME%"
if errorlevel 1 exit /b 1
call :build_target "darwin" "arm64" "%APP_NAME%"
if errorlevel 1 exit /b 1
goto :eof

:: 编译当前平台
:build_current
for /f "delims=" %%i in ('go env GOOS') do set "current_os=%%i"
for /f "delims=" %%i in ('go env GOARCH') do set "current_arch=%%i"
call :log_info "编译当前平台: !current_os!/!current_arch!"
call :build_target "!current_os!" "!current_arch!" "%APP_NAME%"
if errorlevel 1 exit /b 1
goto :eof

:: 编译所有平台
:build_all
call :log_info "开始编译所有平台"
call :build_windows
if errorlevel 1 exit /b 1
call :build_linux
if errorlevel 1 exit /b 1
call :build_macos
if errorlevel 1 exit /b 1
goto :eof

:: 显示构建摘要
:show_summary
echo.
call :log_info "构建摘要:"
echo ----------------------------------------

if exist "%OUTPUT_DIR%" (
    for /r "%OUTPUT_DIR%" %%f in (*.exe aish) do (
        set "file_path=%%f"
        set "relative_path=!file_path:%OUTPUT_DIR%\=!"
        for %%F in ("%%f") do set "file_size=%%~zF"
        echo   !relative_path! (!file_size! bytes)
    )
) else (
    call :log_warning "构建目录不存在"
)

echo ----------------------------------------
call :log_success "构建完成!"
goto :eof

:: 主函数
:main
set "target=all"

:: 解析命令行参数
:parse_args
if "%~1"=="" goto :end_parse
if /i "%~1"=="-h" goto :show_help_and_exit
if /i "%~1"=="--help" goto :show_help_and_exit
if /i "%~1"=="-v" goto :show_version_and_exit
if /i "%~1"=="--version" goto :show_version_and_exit
if /i "%~1"=="-c" goto :clean_and_exit
if /i "%~1"=="--clean" goto :clean_and_exit
if /i "%~1"=="--dev" (
    set "DEV_MODE=true"
    shift /1
    goto :parse_args
)
if /i "%~1"=="all" set "target=all" && shift /1 && goto :parse_args
if /i "%~1"=="windows" set "target=windows" && shift /1 && goto :parse_args
if /i "%~1"=="linux" set "target=linux" && shift /1 && goto :parse_args
if /i "%~1"=="macos" set "target=macos" && shift /1 && goto :parse_args
if /i "%~1"=="current" set "target=current" && shift /1 && goto :parse_args

call :log_error "未知选项: %~1"
goto :show_help_and_exit

:show_help_and_exit
call :show_help
exit /b 0

:show_version_and_exit
echo %APP_NAME% version %VERSION%
exit /b 0

:clean_and_exit
call :clean_build
exit /b 0

:end_parse

:: 检查依赖
call :check_dependencies
if errorlevel 1 exit /b 1

:: 创建输出目录
if not exist "%OUTPUT_DIR%" mkdir "%OUTPUT_DIR%"

:: 根据目标执行编译
if /i "%target%"=="all" call :build_all
if /i "%target%"=="windows" call :build_windows
if /i "%target%"=="linux" call :build_linux
if /i "%target%"=="macos" call :build_macos
if /i "%target%"=="current" call :build_current

if errorlevel 1 exit /b 1

:: 显示摘要
call :show_summary
exit /b 0

:: 执行主函数
call :main %*
